<?php
declare (strict_types = 1);

namespace app\admin\controller\system;
use app\admin\BaseController;
use app\common\model\system\Statement as StateModel;
use think\Exception;

class Statement extends BaseController
{
    //中间件
    protected $middleware = [
        'method:ajax' => ['only'=>['visitedList', 'delete','batchDel']],
    ];

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view('index');
    }

    //访问记录
    public function visitedList(){
        try{
            //获取列表数据
            $data = StateModel::getStatementListData();


        }catch (\Exception $e){
            //系统错误
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage(),
            ]);
        }

        //成功返回
        return json([
            'code'  => 0,
            'msg'=> '查询列表成功',
            'count' => $data['count'],
            'data'  => $data['data']
        ]);
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id)
    {
        try{
            //获取参数
            $res = $this->getModel(StateModel::class, $id)->delete();

            if ($res === false){
                throw new Exception('删除记录失败.');
            }

        }catch (\Exception $e){

            //Log::error($e->getMessage());
            return json(['code'=>1,'msg'=>$e->getMessage()]);
        }
        //删除成功
        return json(['code'=>0,'msg'=>'删除记录成功']);
    }

    /**
     * 批量删除
     */
    public function batchDel(){
        try{
            $ids = explode(',',trim(request()->param('ids'),','));

            if( $this->batchRule(StateModel::class, (array) $ids) &&
                StateModel::destroy($ids) === false )
            {
                throw new Exception('批量删除记录失败.');
            }

        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }

        return json([
            'code' => 0,
            'msg' => '批量删除记录成功.'
        ]);
    }
}
